package com.taptech.doufu.ui.view;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Rect;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ViewFlipper;

import com.klinker.android.link_builder.Link;
import com.klinker.android.link_builder.LinkBuilder;
import com.taptech.doufu.R;
import com.taptech.doufu.ui.activity.NewOthersPersonalActivity;
import com.taptech.doufu.app.WeMediaApplication;
import com.taptech.doufu.constant.UmengEventName;
import com.taptech.doufu.manager.UserLogManager;
import com.taptech.doufu.bean.MarketNovelNoticeBean;
import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.ui.activity.NovelDetailsActivity;
import com.taptech.doufu.ui.activity.SpeakerWallActivity;
import com.taptech.doufu.bean.MsgBean;
import com.taptech.doufu.ui.activity.UserDetailActivity;
import com.taptech.doufu.util.StartActivityUtils;
import com.taptech.doufu.util.TMAnalysis;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AutoScrollNoticeView extends LinearLayout {
    private static final String TAG = AutoScrollNoticeView.class.getSimpleName();
    private Context mContext;
    private ViewFlipper viewFlipper;
    private View scrollTitleView;

    private String pageType;
    private StringBuilder eventName = new StringBuilder();

    public AutoScrollNoticeView(Context context) {
        super(context);
        mContext = context;
        init();
    }

    public AutoScrollNoticeView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        init();
    }

    /**
     * 用于用户日志统计
     * @param pageType
     */
    public void setPageType(String pageType) {
        this.pageType = pageType;
        if (!TextUtils.isEmpty(pageType)) {
            this.eventName.append(pageType).append("-").append("trumpert");
        }else{
            this.eventName.append("trumpert");
        }
    }

    private Link setLink(final String uuid, final String userName, String color, final boolean isWall) {
        Link link = new Link(userName);
        link.setUnderlined(false);
        link.setTextColor(Color.parseColor("#" + color));
        link.setOnClickListener(new Link.OnClickListener() {
            @Override
            public void onClick(String clickedText) {
                umengEvent();
                UserLogManager.addLog(eventName.toString());
                if (isWall) {//to speaker wall
                    startLinkSpeakerWall(uuid);
                } else {// to people page
                    startLinkPerson(uuid);
                }

            }
        });
        return link;
    }


    private void startLinkSpeakerWall(String id) {
        Intent intent = new Intent(mContext, SpeakerWallActivity.class);
        intent.putExtra(Constant.RECORDID, id);
        mContext.startActivity(intent);
    }

    private void startLinkPerson(String uuid) {
        UserDetailActivity.Companion.startActivity(mContext,uuid);
    }

    private void umengEvent() {
        Map<String, String> analysisMap = new HashMap<String, String>();
        analysisMap.put("page_type",pageType);
        TMAnalysis.event(mContext, UmengEventName.HOME_SMALL_HORN, analysisMap);
    }


    /**
     * 小说页面的更新
     *
     * @param mList:
     */
    public void updateScroolNovelNotices(List<MarketNovelNoticeBean> mList) {
        viewFlipper.removeAllViews();
        if ((mList == null) || (mList.size() < 1)) {
            return;
        }

        if ((mList != null) && (mList.size() > 1)) {
            viewFlipper.startFlipping();
        }
        for (final MarketNovelNoticeBean autoScrollViewBean : mList) {
            View scrollView = View.inflate(mContext, R.layout.autoscrollview_item, null);
            TextView tvText = (TextView) scrollView.findViewById(R.id.tv_home_text);
            String textColor ;
            if (autoScrollViewBean.getObject_type().equals("61")) {//speaker wall
                List<Link> links = new ArrayList<>();
                List<MsgBean> lists = MsgBean.getMsgBean(autoScrollViewBean.getMsg());
                for (int i = 0; i < lists.size(); i++) {
                    MsgBean msgBean = lists.get(i);
                    tvText.append(msgBean.getSplit_text());
                    if (WeMediaApplication.getInstance().isDayNightMode_Night) {
                        textColor = msgBean.getNight_color();
                    } else {
                        textColor = msgBean.getDay_color();
                    }
                    if (msgBean.getUid() != null) {
                        links.add(setLink(msgBean.getUid(), msgBean.getSplit_text(), textColor, false));
                    } else {
                        links.add(setLink(autoScrollViewBean.getId(), msgBean.getSplit_text(), textColor, true));
                        tvText.setOnClickListener(new OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                umengEvent();
                                UserLogManager.addLog(eventName.toString());
                                startLinkSpeakerWall(autoScrollViewBean.getId());
                            }
                        });
                    }
                    LinkBuilder.on(tvText).addLinks(links).build();
                }
            } else {
                List<MsgBean> lists = MsgBean.getMsgBean(autoScrollViewBean.getMsg());
                for (int i = 0; i < lists.size(); i++) {
                    MsgBean msgBean = lists.get(i);
                    tvText.append(msgBean.getSplit_text());
                }
                tvText.setText(tvText.getText());
                if (WeMediaApplication.getInstance().isDayNightMode_Night) {
                    tvText.setTextColor(getResources().getColor(R.color.text_dark));
                } else {
                    tvText.setTextColor(getResources().getColor(R.color.text_color_8993A3));
                }
                scrollView.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        umengEvent();
                        //跳转到小说详情
                        MarketNovelNoticeBean.ObjectBean bean = autoScrollViewBean.getData();
                        if (bean != null) {
                            UserLogManager.addLog(UserLogManager.LOG_EVENT_NAME_NOVEL_DETAIL,String.valueOf(bean.getId()),
                                    "小喇叭", pageType,"TFNovelDetail.js");
                            goToNovelDesActivity(bean.getId() + "", bean.getObject_type() + "", bean.getTitle());
                        }
                    }
                });
            }
            viewFlipper.addView(scrollView);
        }
    }

    private void goToNovelDesActivity(String novelId, String novelType, String fromTitleName) {
        TMAnalysis.event(mContext, "novel_laba_click");
        NovelDetailsActivity.Companion.startActivity(mContext,novelId,Intent.FLAG_ACTIVITY_NEW_TASK);
        // 统计事件
        Map<String, String> analysisMap = new HashMap<String, String>();
        analysisMap.put("mpositon", "0");
        analysisMap.put("mid", novelId);
        analysisMap.put("mtype", novelType);
        TMAnalysis.event(mContext, fromTitleName, analysisMap);
    }

    private void init() {
        scrollTitleView = LayoutInflater.from(mContext).inflate(R.layout.list_item_autoscrollview2, null);
        LayoutParams layoutParams = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
        addView(scrollTitleView, layoutParams);

        viewFlipper = (ViewFlipper) scrollTitleView.findViewById(R.id.flipper_home_autoscrollview);
        viewFlipper.setInAnimation(AnimationUtils.loadAnimation(mContext, R.anim.slide_in_up));
        viewFlipper.setOutAnimation(AnimationUtils.loadAnimation(mContext, R.anim.slide_out_up));
    }
}
